#!/bin/sh

case "$1" in
  "stop" )
    kill -2 `cat /var/run/follower_ffmpeg.pid`
    rm -f /var/run/follower_ffmpeg.pid
    sleep 0.5
    kill -9 `cat /var/run/follower.pid`
    rm -f /var/run/follower.pid
    ;;
  "start" )
    cd $HOME
    ./follower </dev/null >/tmp/follower.out 2>& 1 &
    echo $! >/var/run/follower.pid
    ffmpeg -f video4linux2 -s 176x144 -r 8 -i /dev/video0 -f image2 "/tmp/in-bmp%06d.bmp" </dev/null >/tmp/ffmpeg.out 2>& 1 &
    echo $! >/var/run/follower_ffmpeg.pid
    ;;
  * )
    echo "Wrong command! Use 'start' or 'stop' only"
  ;;
esac
